use yew::prelude::*;
use yew_router::prelude::*;

mod api;
mod components;
mod contexts;
mod hooks;
mod pages;

use contexts::CurrentUserProvider;
use pages::{
  crates::{add::CratesAdd, index::Crates},
  home::Home,
  login::Login,
  not_found::NotFound,
  rustaceans::{add::RustaceansAdd, edit::RustaceansEdit, index::Rustaceans},
};

use crate::pages::{
  crates::{delete::CratesDelete, edit::CratesEdit},
  rustaceans::delete::RustaceansDelete,
};

#[derive(Routable, PartialEq, Clone)]
enum Route {
  #[at("/")]
  Home,
  #[at("/roustaceans")]
  Rustaceans,
  #[at("/roustaceans/add")]
  RustaceansAdd,
  #[at("/roustaceans/edit/:id")]
  RustaceansEdit { id: i32 },
  #[at("/roustaceans/delete/:id")]
  RustaceansDelete { id: i32 },
  #[at("/crates")]
  Crates,
  #[at("/crates/add")]
  CratesAdd,
  #[at("/crates/edit/:id")]
  CratesEdit { id: i32 },
  #[at("/crates/delete/:id")]
  CratesDelete { id: i32 },
  #[at("/login")]
  Login,
  #[not_found]
  #[at("/404")]
  NotFound,
}

fn switch(routes: Route) -> Html {
  match routes {
    Route::Login => html! { <Login/> },
    Route::Home => html! { <Home/> },
    Route::Rustaceans => html! { <Rustaceans/> },
    Route::RustaceansAdd => html! { <RustaceansAdd/> },
    Route::RustaceansEdit { id } => {
      html! { <RustaceansEdit rustaceans_id={id}/> }
    }
    Route::RustaceansDelete { id } => {
      html! { <RustaceansDelete rustaceans_id={id}/> }
    }
    Route::Crates => html! { <Crates/> },
    Route::CratesAdd => html! { <CratesAdd/> },
    Route::CratesEdit { id } => {
      html! { <CratesEdit crates_id={id}/> }
    }
    Route::CratesDelete { id } => {
      html! { <CratesDelete crates_id={id}/> }
    }
    Route::NotFound => html! { <NotFound/> },
  }
}

#[function_component]
fn App() -> Html {
  html! {
    <BrowserRouter>
      <CurrentUserProvider>
        <Switch<Route> render={switch} />
      </CurrentUserProvider>
    </BrowserRouter>
  }
}

fn main() {
  yew::Renderer::<App>::new().render();
}
